package com.example.demo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.entity.Coach;
import com.example.demo.entity.CoachAccount;
import com.example.demo.mapper.CoachAccountMapper;
import com.example.demo.mapper.CoachMapper;
import com.example.demo.service.CoachService;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
public class CoachImpl implements CoachService {
    private final CoachMapper coachMapper;
    private final CoachAccountMapper coachAccountMapper;

    @Override
    public Collection<Coach> getAllCoach() {
        List<Coach> allCoach = coachMapper.selectList(null);
        return allCoach;
    }

    @Override
    public List<Coach> filterCoach(Coach coach) {
        QueryWrapper<Coach> queryWrapper = new QueryWrapper<>();
        if (coach.getCoachName() != null && !coach.getCoachName().isEmpty()) {
            queryWrapper.like("coach_name", coach.getCoachName());
        }
        if (coach.getCoachId() != null) {
            queryWrapper.eq("coach_id", coach.getCoachId());
        }
        if (coach.getCoachCar() != null && !coach.getCoachCar().isEmpty()) {
            queryWrapper.like("coach_car", coach.getCoachCar());
        }
        if (coach.getCoachIdentityId() != null && !coach.getCoachIdentityId().isEmpty()) {
            queryWrapper.like("coach_identity_id", coach.getCoachIdentityId());
        }
        if (coach.getCoachSex() != null && !coach.getCoachSex().isEmpty()) {
            queryWrapper.like("coach_sex", coach.getCoachSex());
        }
//        queryWrapper.like("coach_name",coach.getCoachName())
//                .like("coach_id",coach.getCoachId())
//                .like("coach_car",coach.getCoachCar())
//                .like("coach_identity_id",coach.getCoachIdentityId())
//                .like("coach_sex",coach.getCoachSex());
        List<Coach> filterList = coachMapper.selectList(queryWrapper);
        return filterList;
    }

    @Override
    public Coach updateCoach(Coach coach) {
        int result = coachMapper.updateById(coach);
        if(result>0){
            return coach;
        } else {
            throw new RuntimeException("Failed to update coach");
        }
    }

    @Override
    public Coach addCoach(Coach coach) {
        int result = coachMapper.insert(coach);
        if (result > 0) {
            return coach;
        } else {
            throw new RuntimeException("Failed to add coach");
        }
    }

    @Override
    public Coach addCoach(Coach coach, Long coachAccountId) {
        int result = coachMapper.insert(coach);
        Long id=coach.getCoachId();
        UpdateWrapper<CoachAccount> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("coach_account_id",coachAccountId).set("coach_id",id);
        coachAccountMapper.update(updateWrapper);
        if (result > 0) {
            return coach;
        } else {
            throw new RuntimeException("Failed to add coach");
        }
    }

    @Override
    public Long deleteCoach(Long coachId) {
        QueryWrapper<Coach> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("coach_id", coachId);
        int result = coachMapper.delete(queryWrapper);
        if (result > 0) {
            return coachId;
        } else {
            throw new RuntimeException("Failed to delete coach");
        }
    }

    @Override
    public Coach getCoachByCoachId(Long coachId) {
        return coachMapper.findById(coachId);
    }

    @Override
    public CoachAccount getCoachAccountByCoachId(Long coachId) {
        return coachAccountMapper.findByCoachId(coachId);
    }

    @Override
    public CoachAccount updateSelfAccount(CoachAccount coachAccount) {
        int result = coachAccountMapper.updateById(coachAccount);
        if (result>0){
            return coachAccount;
        }
        return null;
    }

    @Override
    public List<Coach> findBySexOrAge(String sex, String age1, String age2) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        if(!sex.equals("null")) {
            queryWrapper.eq("coach_sex", sex);
        }
        if( !age1.equals("null") && !age2.equals("null") ) {
//            System.out.println(age1);
//            System.out.println(age2);
            queryWrapper.between("coach_age", age1, age2);
        }
        else if (!age1.equals("null")&&age2.equals("null")) {
            queryWrapper.ge("coach_age",age1);
        }
        else if (age1.equals("null")&&!age2.equals("null")) {
            queryWrapper.le("coach_age",age2);
        }
        if (queryWrapper==null)return coachMapper.selectList(null);
        return coachMapper.selectList(queryWrapper);
    }
}
